package com.lpe.sys.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lipain on 2017/12/19.
 */
public class URLInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(URLInterceptor.class);

    //在请求处理之前进行调用（Controller方法调用之前）
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String ip = httpServletRequest.getRemoteHost();
        System.err.println(ip);
        return true;
    }

    //请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
//        List<BlackList> blackLists = blackListDao.findByIp(ip);
//        if (blackLists == null || blackLists.size() == 0){
//            urlHandle(httpServletRequest, 5000, 10);
//        } else {
//            //强制控制跳转
//            modelAndView.setViewName("/errorpage/error.html");
//        }
    }

    //在整个请求结束之后被调用
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

//    public void urlHandle(HttpServletRequest request, long limitTime,int limitCount) throws RequestLimitException {
//        /**
//         * 省略业务逻辑部分，参考"准备"步骤
//         */
//        if (count > limitCount){ //符合锁定条件
//            Calendar calendar = Calendar.getInstance();
//            Date iptime=calendar.getTime();
//            BlackList blackList = new BlackList(ip, iptime);
//            blackListDao.addBlackList(blackList);
//            throw new RequestLimitException();
//        }
//    }
}
